let WebStorage = {
    session: {
        set (key, value) {
            if (typeof value !== 'string') {
                try {
                    value = JSON.stringify(value)
                } catch (e) {
                    throw new Error('Data type error!')
                }
            }
            sessionStorage.setItem(key, value)
        },
        get (key) {
            let value = sessionStorage.getItem(key)
            try {
                return JSON.parse(value)
            } catch (e) {
                return value
            }
        },
        remove (key) {
            sessionStorage.removeItem(key)
        },
    },
    local: {
        set (key, value) {
            if (typeof value !== 'string') {
                try {
                    value = JSON.stringify(value)
                } catch (e) {
                    throw new Error('Data type error!')
                }
            }
            localStorage.setItem(key, value)
        },
        get (key) {
            let value = localStorage.getItem(key)
            try {
                return JSON.parse(value)
            } catch (e) {
                return value
            }
        },
        remove (key) {
            localStorage.removeItem(key)
        },
    },
    getCart () {
        let cart = this.local.get('bwc_cart')
        let domainKey = location.pathname.split('/')[1]
        if (!cart) {
            cart = {}
        }
        if (!cart[domainKey]) {
            return []
        } else {
            return cart[domainKey]
        }
    },
    addToCart (cartData) {
        let cartHistory = this.local.get('bwc_cart')
        let domainKey = location.pathname.split('/')[1]
        if (!cartHistory || cartHistory.constructor !== Object) {
            cartHistory = {}
        }
        let currentUserCart = cartHistory[domainKey]
        if (!currentUserCart) {
            currentUserCart = []
        }
        loop: for (let newData of cartData) {
            for (let history of currentUserCart) {
                if (history.item.id === newData.item.id) {
                    history.count += newData.count
                    continue loop
                }
            }
            currentUserCart.push(newData)
        }
        cartHistory[domainKey] = currentUserCart
        this.setCart(cartHistory)
    },
    setCart (data) {
        this.local.set('bwc_cart', data)
    },
    removeCartByIds (ids) {
        let cartHistory = this.local.get('bwc_cart')
        let domainKey = location.pathname.split('/')[1]
        let currentUserCart = cartHistory[domainKey]
        let cartNew = []
        for (let item of currentUserCart) {
            if (ids.indexOf(item.item.id) === -1) {
                cartNew.push(item)
            }
        }
        this.setCart(cartNew)
    },
    clearCart () {
        let cart = {}
        let domainKey = location.pathname.split('/')[1]
        cart[domainKey] = []
        this.setCart(cart)
    },
    clearCartByIds (ids) {
        let cartHistory = this.local.get('bwc_cart')
        let domainKey = location.pathname.split('/')[1]
        let currentUserCart = cartHistory[domainKey]
        let newCartTemp = {}
        newCartTemp[domainKey] = currentUserCart.filter((item) => {
            for (let skuId of ids) {
                if (item.item.id === skuId) {
                    return false
                }
            }
            return true
        })
        this.setCart(newCartTemp)
    },
}

export default WebStorage